package stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author du.senlin
 * @Title:
 * @Description:
 * @date 2020/3/30 11:46
 */
public class StreamDemo {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i + "");
        }
        list.add(66 + "a");
        list.add(66 + "a");
        list.add(1 + "");
        // 去除空格
        filter1(list);
        // 过滤
        filter2(list);
        // 统计次数
        list.stream().filter((s) -> s.startsWith("1")).count();
        // 排序
        list.stream().sorted().filter((s) -> s.endsWith("6")).forEach(System.out::println);
        // 去重复
        list.stream().distinct().collect(Collectors.toList()).forEach(System.out::println);
        // map 用于映射每个元素到对应的结果，以下代码片段使用 map 输出了元素对应的大写
        replace(list);
        // Collectors
        collectors(list);
        // 交集、并集、差集
        other();

    }

    private static void collectors(List<String> list) {
        List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());

        System.out.println("筛选列表: " + filtered);
        String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
        System.out.println("合并字符串: " + mergedString);
    }

    private static void replace(List<String> list) {
        // 将字符串转换为大写字符串。
        // 也可以通过map来讲对象转换成其他类型，
        // map返回的Stream类型是根据你map传递进去的函数的返回值决定的
        list.stream()
                .map(String::toUpperCase)
                .sorted((a, b) -> b.compareTo(a))
                .forEach(System.out::println);

        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        // 获取对应的平方数并且去重复
        List<Integer> squaresList = numbers.stream().map(i -> i * i).distinct().collect(Collectors.toList());

    }

    private static void filter2(List<String> list) {
        list.stream().filter((s) -> s.startsWith("1")).forEach(System.out::println);
    }

    private static void filter1(List<String> list) {
        list.add("");
        List<String> filtered = list.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
        System.out.println(filtered);
    }


    public static void other(){
        List<Integer> list1=new ArrayList<>();
        list1.add(1);
        list1.add(2);
        list1.add(3);

        List<Integer> list2=new ArrayList<>();
        list2.add(3);
        list2.add(6);
        list2.add(5);

        System.out.println("交集=");

        List<Integer> list=list1.stream().filter(t->list2.contains(t)).collect(Collectors.toList());
        list.stream().forEach(System.out::println);




        System.out.println("差集=");
        list=list1.stream().filter(t-> !list2.contains(t)).collect(Collectors.toList());
        list.stream().forEach(System.out::println);


        System.out.println("并集=");

        list.addAll(list1);
        list.addAll(list2);
        list=list.stream().distinct().collect(Collectors.toList());
        list.stream().forEach(System.out::println);
    }


}
